package day10.RandomAccessFileTest;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

public class Test2 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("/Users/hao/Desktop/abc/f2", "rw");
        raf.write(97); // 00 00 00 61 --> 61
        raf.write(98); // 00 00 00 62 --> 62
        raf.write(99); // 00 00 00 63 --> 63
        raf.write(356);// 00 00 01 64 --> 64

        byte[] a = {
                101,102,103,104,105,
                106,107,108,109,110
        };
        raf.write(a);
        raf.write(a, 2, 4); // 从下标开始的第四个


        raf.seek(0);
        int b;
        while((b = raf.read()) != -1){ //制式写法
            System.out.println(b);
        }

        // 重新定位下标为0
        raf.seek(0);
        byte[] buff = new byte[5];
        while((b = raf.read(buff)) != -1){
            System.out.println(b +" "+ Arrays.toString(buff)); //每一次循环会把上一次的覆盖掉
            /*
            5 [97, 98, 99, 100, 101]
            5 [102, 103, 104, 105, 106]
            5 [107, 108, 109, 110, 103]
            3 [104, 105, 106, 110, 103]
             */
        }
        raf.close(); // 释放系统资源
    }
}
